/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 18.05.13
 * Time: 0:23
 * To change this template use File | Settings | File Templates.
 */
package {
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TextEvent;
import flash.geom.Point;

import mx.collections.ArrayCollection;

import mx.controls.Button;
import mx.controls.Label;

import mx.controls.List;

import mx.core.Application;
import mx.core.UIComponent;
import mx.events.FlexEvent;
import mx.events.ListEvent;

import spark.components.TextInput;

public class VirtualLabWorkTemplate extends Application {
    public var labWorkNameLabel:Label;
    public var objectsList:List;
    public var addObjectButton:Button;
    public var removeObjectButton:Button;
    public var mainCanvas:UIComponent;
    public var physObjectsCanvas:UIComponent;
    public var startButton:Button;
    public var stopButton:Button;
    public var resetButton:Button;

    public var objectNameLabel:Label;
    public var objectNameTextInput:TextInput;

    public var physObjsArrColl:ArrayCollection = new ArrayCollection();
    public var currentPhysObject:PhysicsObject;
    private var _axisCenter:Point;
    protected var isPlay:Boolean = false;

    public function VirtualLabWorkTemplate() {
        layout = "absolute";

        if (stage) initComponents();
        else this.addEventListener(Event.ADDED_TO_STAGE, initComponents);
    }

    private function initComponents(event:Event = null):void {
        labWorkNameLabel = new Label();
        labWorkNameLabel.x = 10;
        labWorkNameLabel.y = 10;
        labWorkNameLabel.width = 400;
        addElement(labWorkNameLabel);

        objectsList = new List();
        objectsList.x = 10;
        objectsList.y = 36;
        objectsList.width = 230;
        objectsList.height = 375;
        objectsList.labelField = "identifier";
        objectsList.addEventListener(ListEvent.CHANGE, onSelectItem);
        addElement(objectsList);

        addObjectButton = new Button();
        addObjectButton.x = 10;
        addObjectButton.y = 419;
        addObjectButton.label = "Добавить";
        addObjectButton.width = 100;
        addObjectButton.addEventListener(MouseEvent.CLICK, function (event:Event):void {
            addPhysObject();
        });
        addElement(addObjectButton);

        removeObjectButton = new Button();
        removeObjectButton.x = 140;
        removeObjectButton.y = 419;
        removeObjectButton.label = "Удалить";
        removeObjectButton.width = 100;
        removeObjectButton.addEventListener(MouseEvent.CLICK, function (event:Event):void {
            removePhysObject();
        });
        addElement(removeObjectButton);

        mainCanvas = new UIComponent();
        mainCanvas.x = 248;
        mainCanvas.y = 36;
        mainCanvas.width = 540;
        mainCanvas.height = 405;
        addElement(mainCanvas);

        physObjectsCanvas = new UIComponent();
        mainCanvas.addChild(physObjectsCanvas);

        startButton = new Button();
        startButton.x = 10;
        startButton.y = 478;
        startButton.label = "Пуск";
        startButton.width = 100;
        startButton.addEventListener(MouseEvent.CLICK, function (event:Event):void {
            startLab();
        });
        addElement(startButton);

        stopButton = new Button();
        stopButton.x = 10;
        stopButton.y = 506;
        stopButton.label = "Пауза";
        stopButton.width = 100;
        stopButton.addEventListener(MouseEvent.CLICK, function (event:Event):void {
            stopLab();
        });
        addElement(stopButton);

        resetButton = new Button();
        resetButton.x = 10;
        resetButton.y = 535;
        resetButton.label = "Сброс";
        resetButton.width = 100;
        resetButton.addEventListener(MouseEvent.CLICK, function (event:Event):void {
            resetLab();
        });
        addElement(resetButton);

        objectNameLabel = new Label();
        objectNameLabel.x = 248;
        objectNameLabel.y = 451;
        objectNameLabel.text = "Название объекта";
        addElement(objectNameLabel);

        objectNameTextInput = new TextInput();
        objectNameTextInput.x = 400;
        objectNameTextInput.y = 449;
        objectNameTextInput.width = 100;
        objectNameTextInput.addEventListener(Event.CHANGE, changeName);
        addElement(objectNameTextInput);

        initLab();
    }

    private function changeName(event:Event):void {
        if (objectsList.selectedIndex > -1) {
            (physObjsArrColl.getItemAt(objectsList.selectedIndex) as PhysicsObject).identifier = event.currentTarget.text;
            objectsList.dataProvider.refresh();
        }
    }

    public function onSelectItem(event:Event):void {
        currentPhysObject = event.currentTarget.selectedItem; // physicsObjectsCollection.getItemAt(objectsList.selectedIndex) as PhysicsObject;
    }

    public function addPhysObject():void {
        // Override me!!
    }

    public function removePhysObject():void {
        if (objectsList.selectedIndex > -1) {
            var item:PhysicsObject = physObjsArrColl.getItemAt(objectsList.selectedIndex) as PhysicsObject;
            physObjectsCanvas.removeChild(item);
            physObjsArrColl.removeItemAt(objectsList.selectedIndex);
        }
    }

    public function initLab():void {
        // Override me!!
        axisCenter = new Point(50, 360);
        drawBorder();
        drawAxis();
    }

    public function startLab():void {
        trace("Start lab...");
        if (stage) {
            stage.addEventListener(Event.ENTER_FRAME, updateLab);
            isPlay = true;
        }
    }

    public function updateLab(event:Event):void {
        // Override me!!
    }

    public function stopLab():void {
        trace("Stop lab...");
        if (stage) {
            stage.removeEventListener(Event.ENTER_FRAME, updateLab);
            isPlay = false;
        }
    }

    public function resetLab():void {
        stopLab();
        trace("Reset lab...");
        // Override me!!
    }

    /* CANVAS MANAGEMENT */
    protected function drawBorder():void {
        mainCanvas.graphics.lineStyle(2, 0x0);
        mainCanvas.graphics.moveTo(0, 0);
        mainCanvas.graphics.lineTo(mainCanvas.width, 0);
        mainCanvas.graphics.lineTo(mainCanvas.width, mainCanvas.height);
        mainCanvas.graphics.lineTo(0, mainCanvas.height);
        mainCanvas.graphics.lineTo(0, 0);
    }

    protected function drawAxis():void {
        mainCanvas.graphics.lineStyle(2, 0x0);
        mainCanvas.graphics.moveTo(10, axisCenter.y);
        mainCanvas.graphics.lineTo(mainCanvas.width - 10, axisCenter.y);

        mainCanvas.graphics.moveTo(axisCenter.x, 10);
        mainCanvas.graphics.lineTo(axisCenter.x, mainCanvas.height - 10);
    }
    
    protected function resetPhysObjectsCanvas():void {
        physObjectsCanvas.x = axisCenter.x;
        physObjectsCanvas.y = axisCenter.y;
    }

    public function get axisCenter():Point {
        return _axisCenter;
    }

    public function set axisCenter(value:Point):void {
        _axisCenter = value;
    }
}
}
